package com.punch.course.controller;


import com.punch.common.dto.course.CourseAuditDTO;
import com.punch.common.dto.course.CourseDTO;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.vo.common.OptionVO;
import com.punch.common.vo.course.CourseContentVO;
import com.punch.common.vo.course.temp.CoursePageVO;
import com.punch.common.vo.course.temp.CourseVO;
import com.punch.common.vo.course.temp.SimpleCourseVO;
import com.punch.course.exception.CourseExceptionEnum;
import com.punch.course.query.CourseQueryInfo;
import com.punch.course.service.CourseTempService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 课程模板表 前端控制器
 * </p>
 *
 * @author wjj
 * @since 2020-12-01
 */
@RestController
public class CourseTempController {

    @Resource
    private CourseTempService courseTempService;


    /**
     * 条件分页查询课程列表
     *
     * @return IPage<CoursePageVO>
     * @author xiaol
     * @since 2020/7/14 10:51
     */
    @GetMapping(value = "/courses", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "条件查询课程列表", notes = "条件查询课程列表", produces = "application/json")
    public Result<PageInfo<CoursePageVO>> listCourse(@ModelAttribute CourseQueryInfo queryInfo) {
        return Result.succeed(courseTempService.listCourse(queryInfo));
    }


    /**
     * 查询课程详情
     *
     * @param courseId 课程id
     * @return CourseVO
     */
    @GetMapping(value = "/course/{courseId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询课程列表详情", notes = "查询课程列表详情", produces = "application/json")
    public Result<CourseVO> getCourse(@PathVariable("courseId") Long courseId) {
        return Result.succeed(courseTempService.getCourse(courseId));
    }

    /**
     * 保存课程
     *
     * @param courseDTO 课程表单
     * @return 课程id
     */
    @PostMapping(value = "/course", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "保存课程信息", notes = "保存课程信息", produces = "application/json")
    public Result<Long> saveCourse(@Valid @RequestBody CourseDTO courseDTO) {
        Long courseId = courseTempService.saveCourse(courseDTO);
        return Result.succeed(courseId, CourseExceptionEnum.COURSE_SAVE_SUCCESS.getMsg());
    }

    /**
     * 修改课程
     *
     * @param courseDTO 课程表单
     * @return 课程id
     */
    @PutMapping(value = "/course", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "编辑课程信息", notes = "编辑课程信息", produces = "application/json")
    public Result<Long> updateCourse(@Valid @RequestBody CourseDTO courseDTO) {
        Long courseId = courseTempService.updateCourse(courseDTO);
        return Result.succeed(courseId, CourseExceptionEnum.COURSE_EDIT_SUCCESS.getMsg());
    }

    /**
     * 删除课程
     *
     * @param courseId 课程id
     */
    @DeleteMapping(value = "/course/{courseId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除课程信息", notes = "删除课程信息", produces = "application/json")
    public Result<String> deleteCourse(@PathVariable Long courseId) {
        courseTempService.deleteCourse(courseId);
        return Result.succeed(CourseExceptionEnum.COURSE_DEL_SUCCESS.getMsg());
    }

    /**
     * 课程审核
     *
     * @param courseAuditDTO 课程信息
     * @return courseId
     * @author xiaol
     * @since 2020/7/21
     */
    @PutMapping(value = "/course/audit", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "课程审核", notes = "课程审核", produces = "application/json")
    public Result auditCourse(@RequestBody CourseAuditDTO courseAuditDTO) {
        courseTempService.auditCourse(courseAuditDTO);
        return Result.succeed(CourseExceptionEnum.COURSE_EDIT_SUCCESS.getMsg());
    }

    /**
     * 获取课程审核失败原因
     *
     * @param courseId 课程id
     * @return string
     */
    @GetMapping("/course/{courseId}/audit-message")
    @ApiOperation(value = "获取课程审核失败原因")
    public Result<String> getCourseAuditMessage(@PathVariable Long courseId) {
        return Result.succeed(courseTempService.getCourseAuditMessage(courseId), "");
    }

    /**
     * 课程课节列表
     *
     * @param courseId 课程id
     * @return list
     */
    @GetMapping(value = "/course/{courseId}/lessons", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "课程课节列表", notes = "课程课节列表", produces = "application/json")
    public Result<List<CourseContentVO>> listCourseLessons(@PathVariable Long courseId) {
        return Result.succeed(courseTempService.listCourseLessons(courseId));
    }


    /**
     * 课程下拉列表
     *
     * @param courseName 课程名称
     * @return List<SimpleCourseVO>
     */
    @GetMapping(value = "/courses/simple", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "课程下拉列表", notes = "课程下拉列表", produces = "application/json")
    public Result<List<SimpleCourseVO>> listCourseSelect(@RequestParam(name = "courseName", required = false) String courseName) {
        return Result.succeed(courseTempService.listCourseSelect(courseName));
    }

    /**
     * 修改机构未审核状态下提交的课程
     *
     * @param orgId 机构id
     */
    @PutMapping(value = "/courses/un-audit-commit/{orgId}", produces = "application/json; charset=utf-8")
    public void updateUnAuditCommit(@PathVariable("orgId") Long orgId) {
        courseTempService.updateUnAuditCommit(orgId);
    }

    /**
     * 地区下课程列表
     *
     * @param areaId 地区id
     * @return List<OptionVO < Integer>>
     */
    @GetMapping(value = "/courses/simple/{areaId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "地区下课程列表", notes = "地区下课程列表", produces = "application/json")
    public Result<List<OptionVO<String>>> listSimpleCourse(@PathVariable("areaId") Integer areaId) {
        return Result.succeed(courseTempService.listSimpleCourse(areaId));
    }
}
